package com.example.myapplication.adapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.food_message.Menu;
import java.util.ArrayList;
import java.util.List;

public class MennuAdapter extends BaseAdapter {
    private Context mContext;
    List<Menu> mlList = new ArrayList<>();


    public MennuAdapter(Context mContext, List<Menu> mealList) {
        this.mContext = mContext;
        this.mlList = mealList;
    }

    @Override
    public int getCount() {
        return mlList.size();
    }

    @Override
    public Object getItem(int position) {
        return mlList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //a1 数量, a2 价格, a3 总价;
        int a1, a2, a3;
        //s1为数量/份数  s2为价格
        String s1, s2, s3;
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_home, null);
            viewHolder = new ViewHolder();
            viewHolder.mTvMealName = (TextView) convertView.findViewById(R.id.list_menuname);
            viewHolder.mTvMealSize = (TextView) convertView.findViewById(R.id.list_menusize);
            viewHolder.mTvMealMoney = (TextView) convertView.findViewById(R.id.list_menunomey);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        a1 = mlList.get(position).getmSize();
        a2 = mlList.get(position).getmMoney();
        a3 = a1 * a2;
        s1 = String.valueOf(a1);
        s2 = String.valueOf(a3);
        viewHolder.mTvMealName.setText(mlList.get(position).getmName());
        viewHolder.mTvMealSize.setText("X" + s1);
        viewHolder.mTvMealMoney.setText("￥" + s2);
        return convertView;
    }

    public void clear() {
        mlList.clear();
        notifyDataSetChanged();
    }

    public static class ViewHolder {
        //   ImageView mImageView;
        TextView mTvMealName;
        TextView mTvMealSize;
        TextView mTvMealMoney;
    }

}
